//
//  ReweetedStatusView.swift
//  SinaWebo
//
//  Created by mis on 16/3/21.
//  Copyright © 2016年 mis. All rights reserved.
//

import UIKit
import SnapKit

class ReweetedStatusView: UIView {
    var bottomConstraint: Constraint?
    
    var retweetedStatus: Status? {
        didSet {
            retweenLabel.attributedText = EmoticonManager.emoticonManager.textToImageText(retweetedStatus?.text ?? "", font: retweenLabel.font)
            self.bottomConstraint?.uninstall()
            if let picurls = retweetedStatus?.imageURLs where picurls.count > 0 {
                pictureView.imageURLs = picurls
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(retweenLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 0.98, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(retweenLabel)
        addSubview(pictureView)
        
        //设置约束
        retweenLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweenLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweenLabel.snp_left)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
    }
    
    private lazy var retweenLabel = UILabel.init(title: "我是转发微博", size: 14, color: UIColor.lightGrayColor(), margin: StatusCellMargin)
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}
